package coolq.yaozhenyong.controller;

import java.util.List;

import com.forte.qqrobot.anno.timetask.CronTask;
import com.forte.qqrobot.beans.messages.result.GroupList.Group;
import com.forte.qqrobot.sender.MsgSender;
import com.forte.qqrobot.timetask.TimeJob;
import com.forte.qqrobot.utils.CQCodeUtil;

import coolq.yaozhenyong.bean.GroupList;
import coolq.yaozhenyong.custom.SpringContextUtil;
import coolq.yaozhenyong.custom.TimingOnes;
import coolq.yaozhenyong.service.GroupListService;

@CronTask("0/30 * * * * ? *")
public class TimingOne implements TimeJob{

	@Override
	public void execute(MsgSender msgSender, CQCodeUtil cqCodeUtil) {
		/****
		  更新机器人加入的群
		 */
		Integer updategrade = TimingOnes.getUpdategrade();
		if(updategrade == 2 ) {
			TimingOnes.setUpdategrade(1);
			GroupListService bean = SpringContextUtil.getBean(GroupListService.class);
			/** 获取数据库所有的群  **/
			com.forte.qqrobot.beans.messages.result.GroupList groupList = msgSender.GETTER.getGroupList();
			Group[] Grouplist = groupList.getList();
			for(int j=0;j<Grouplist.length;j++) {
				GroupList one = bean.query().eq("groupcode", Grouplist[j].getCode()).one();
				if(one==null) {
					GroupList grouplists = new GroupList();
					grouplists.setName(Grouplist[j].getName());
					grouplists.setGroupcode(Grouplist[j].getCode());
					grouplists.setIsfalsetrue("在群");
					bean.save(grouplists);
				}
			}
			List<GroupList> list = bean.list();
			
			for (int i = 0; i < list.size(); i++) {
				Boolean isfalsetrue = false;
				for (int j = 0; j < Grouplist.length; j++) {
					if(list.get(i).getGroupcode().equals(Grouplist[j].getCode())) {
						if(list.get(i).getIsfalsetrue().equals("离群")||list.get(i).getIsfalsetrue().equals(null)) {
							GroupList groupList2 = new GroupList();
							groupList2.setId(list.get(i).getId());
							groupList2.setIsfalsetrue("在群");
							bean.updateById(groupList2);
						}
						isfalsetrue = true;
						break;
					}	
				}
				if(isfalsetrue ==false) {
					GroupList groupList2 = new GroupList();
					groupList2.setId(list.get(i).getId());
					groupList2.setIsfalsetrue("离群");
					bean.updateById(groupList2);
				}
			}
				
		}
	}

}
